import { userAPI } from '@/service/api';
import CryptoJS from 'crypto-js';
import { AUTHORIZATION_KEY, INVITE_CODE_KEY, NICK_NAME_KEY } from '@/const/application.ts';
import { userTransactionPage } from '@/service/api/user.ts';

export async function queryGzhQrCode(sceneKey: string,inviteCode?:string) {
  return userAPI.queryGzhQrCode(sceneKey,inviteCode);
}

export async function queryLoginStatus(sceneKey: string) {
  return userAPI.queryLoginStatus(sceneKey);
}

export async function login(data: Parameters<typeof userAPI.login>[0]) {
  const userName = data.userName;
  let password = CryptoJS.MD5(data.password).toString();
  password = CryptoJS.MD5(data.password).toString();
  const res = await userAPI.login({ userName, password });
  if (res.success) {
    window.localStorage.setItem(NICK_NAME_KEY, res.result.nickName);
    window.localStorage.setItem(AUTHORIZATION_KEY, res.result.token);
  }
  return res;
}

export function logout() {
  window.localStorage.removeItem(AUTHORIZATION_KEY);
  window.localStorage.removeItem(NICK_NAME_KEY);
  window.localStorage.removeItem(INVITE_CODE_KEY);
  return Promise.resolve(true);
}

export function check() {
  const auth = window.localStorage.getItem(AUTHORIZATION_KEY);
  if (auth) {
    return Promise.resolve(true);
  }
  return Promise.resolve(false);
}

export function getAuth() {
  return window.localStorage.getItem(AUTHORIZATION_KEY);
}

export async function queryUserInfo() {
  return userAPI.queryUserInfo();
}

export async function queryUserTransactionPage(params: Parameters<typeof userAPI.userTransactionPage>[0]) {
  return userAPI.userTransactionPage(params);
}

export async function queryInviteRecordPage(params: Parameters<typeof userAPI.queryInviteRecordPage>[0]) {
  return userAPI.queryInviteRecordPage(params);
}